package com.tfswx.ruleengine.database.bean;

import cn.hutool.extra.spring.SpringUtil;
import com.tfswx.ruleengine.database.bean.table.BdsTable;
import com.tfswx.ruleengine.database.bean.table.FfTable;
import com.tfswx.ruleengine.database.bean.table.GzdyTable;
import com.tfswx.ruleengine.database.bean.table.GzflTable;
import com.tfswx.ruleengine.database.bean.table.MxstgxTable;
import com.tfswx.ruleengine.database.bean.table.StgxTable;
import com.tfswx.ruleengine.database.config.DataSourceConfig;
import com.tfswx.ruleengine.database.enums.SjklxEnum;
import com.tfswx.ruleengine.database.service.GzyqService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author Ricky
 * @date 2021/7/10
 */

public class TableManager {

    private static final Map<String, BaseTable> tableMap = new HashMap<>();

    private static final GzyqService gzyqService = SpringUtil.getBean(GzyqService.class);

    static {
        // 规则分类表
        tableMap.put("gzfl", new GzflTable());
        // 规则定义表
        tableMap.put("gzdy", new GzdyTable());
        // 表达式表
        tableMap.put("bds", new BdsTable());
        // 模型实体关系表
        tableMap.put("mxstgx", new MxstgxTable());
        // 实体关系表
        tableMap.put("stgx", new StgxTable());
        // 方法表
        tableMap.put("ff", new FfTable());
    }

    private TableManager() {
    }

    public static BaseTable getTable(String name) {
        return tableMap.get(name);
    }

    public static void createTable() {
        createTable(null);
    }

    public static void createTable(String tableName) {
        String databaseId = DataSourceConfig.getDatabaseId();
        Set<Map.Entry<String, BaseTable>> entries = tableMap.entrySet();
        for (Map.Entry<String, BaseTable> entry : entries) {
            String key = entry.getKey();

            if (tableName != null && !tableName.equalsIgnoreCase(key)) {
                continue;
            }

            BaseTable table = entry.getValue();
            SjklxEnum sjklxEnum = SjklxEnum.getSjklxEnum(databaseId);
            List<String> sqls = table.getCreateSql(sjklxEnum);
            for (String sql : sqls) {
                gzyqService.execute(sql);
            }
        }
    }


    public static void dropTable() {
        dropTable(null);
    }

    public static void dropTable(String tableName) {
        String databaseId = DataSourceConfig.getDatabaseId();
        Set<Map.Entry<String, BaseTable>> entries = tableMap.entrySet();
        for (Map.Entry<String, BaseTable> entry : entries) {
            String key = entry.getKey();

            if (tableName != null && !tableName.equalsIgnoreCase(key)) {
                continue;
            }

            BaseTable table = entry.getValue();
            SjklxEnum sjklxEnum = SjklxEnum.getSjklxEnum(databaseId);
            List<String> sqls = table.getDropSql(sjklxEnum);
            for (String sql : sqls) {
                gzyqService.execute(sql);
            }
        }
    }


    public static void truncateTable() {
        truncateTable(null);
    }

    public static void truncateTable(String tableName) {
        String databaseId = DataSourceConfig.getDatabaseId();
        Set<Map.Entry<String, BaseTable>> entries = tableMap.entrySet();
        for (Map.Entry<String, BaseTable> entry : entries) {
            String key = entry.getKey();

            if (tableName != null && !tableName.equalsIgnoreCase(key)) {
                continue;
            }

            BaseTable table = entry.getValue();
            SjklxEnum sjklxEnum = SjklxEnum.getSjklxEnum(databaseId);
            List<String> sqls = table.getTruncateSql(sjklxEnum);
            for (String sql : sqls) {
                gzyqService.execute(sql);
            }
        }
    }

}
